subroutine hitung_soal1(metode,n)
! metode : pilihan metode (1-9)
! n : jumlah pias
! hasil : variabel output keluaran hasil
	implicit none
	integer*2 metode, n
	double precision soal1
	external soal1, simpson_1_per_3, simpson_3_per_8
	external ATURAN_TRAPESIUM, ATURAN_TITIKTENGAH, ROMBERG, ATURAN_GL
	double precision a, b, hasil
	a = 0
	b = 100
	
	select case (metode)
		case (1)
			print*,"Metode Aturan Trapesium"
			call ATURAN_TRAPESIUM(a,b,n,hasil,soal1)
			print*,"Solusi = ",hasil
		case (2)
			print*,"Metode Aturan Titik Tengah"
			call ATURAN_TITIKTENGAH(a,b,n,hasil,soal1)
			print*,"Solusi = ",hasil
		case (3)
			if (mod(n,2).eq.0) then
				print*,"Metode Aturan 1/3 Simpson"
				call simpson_1_per_3(a,b,n,hasil,soal1)
				print*,"Solusi = ",hasil
			else
				print*,"Nilai n = ",n," tidak bernilai genap"
			end if
		case (4)
			if (mod(n,3).eq.0) then
				print*,"Metode Aturan 3/8 Simpson"
				call simpson_3_per_8(a,b,n,hasil,soal1)
				print*,"Solusi = ",hasil
			else
				print*,"Nilai n = ",n," tidak bernilai kelipatan tiga"
			end if
		case (5)
			print*,"Metode Romberg"
			call ROMBERG(a,b,n,hasil,soal1)
			print*,"Solusi = ",hasil
		case (6)
			print*,"Metode Gauss Legendre 3 titik"
			call ATURAN_GL(a,b,hasil,3,soal1)
			print*,"Solusi = ",hasil
		case (7)
			print*,"Metode Gauss Legendre 4 titik"
			call ATURAN_GL(a,b,hasil,4,soal1)
			print*,"Solusi = ",hasil
		case (11)
			print*,"Metode Gauss Legendre 5 titik"
			call ATURAN_GL(a,b,hasil,5,soal1)
			print*,"Solusi = ",hasil
		case (9)
			print*,"Metode Gauss Legendre 6 titik"
			call ATURAN_GL(a,b,hasil,6,soal1)
			print*,"Solusi = ",hasil
	end select
	
end

subroutine hitung_soal2(metode,n)
! metode (1-3)
! n jumlah pias
	implicit none
	integer*2 metode, n, r
	double precision soal2_xtetap
	external soal2_xtetap
	external ATURAN_TRAPESIUM, simpson_1_per_3, ATURAN_GL
	double precision a, b, hasil, delta
	double precision x, x_tetap, sigma, temp
	! gauss legendre 2 titik
	double precision f1, f2
	common /xtetap/ x_tetap
	!a,b untuk arah x dan y
	a = 0
	b = 1
	!jarak antar titik dalam arah x dan y
	delta = (b-a)/n
	x = a
	r = 1
	
	select case (metode)
		case (1)
			print*,"Metode Aturan Trapesium"
			!aturan trapesium untuk arah y, x tetap
			x_tetap = x
			sigma = 0
			do r=1,n+1
				call ATURAN_TRAPESIUM(a,b,n,temp,soal2_xtetap)
				!print*,"Untuk x = ",x_tetap," temp = ",temp
				x_tetap = x_tetap + delta
				if((r.eq.1).or.(r.eq.(n+1))) then
					sigma = sigma + temp
				else
					sigma = sigma + 2*temp
				end if
			end do
			hasil = sigma*delta/2
			print*,"Solusi ",hasil
		case (2)
			if (mod(n,2).eq.0) then
				print*,"Metode Aturan 1/3 Simpson"
				!aturan simpson_1_per_3 untuk arah y, x tetap
				x_tetap = x
				sigma = 0
				do r=1,n+1
					call simpson_1_per_3(a,b,n,temp,soal2_xtetap)
					!print*,"Untuk x = ",x_tetap," temp = ",temp
					x_tetap = x_tetap + delta
					if((r.eq.1).or.(r.eq.(n+1))) then
						sigma = sigma + temp
					else if (mod(r-1,2).eq.1) then
						sigma = sigma + 4*temp
						!print*,"ganjil"
					else
						sigma = sigma + 2*temp
						!print*,"genap"
					end if
				end do
				hasil = sigma*delta/3
				print*,"Solusi ",hasil
			else
				print*,"Nilai n = ",n," tidak bernilai genap"
			end if
		case (3)
			print*,"Metode Gauss Legendre 2 titik"
			! x tetap untuk nilai sqrt(3)/3 dan -sqrt(3)/3
			x_tetap = ((a + b) + (b - a) * sqrt(3.D0)/3) / 2
			call ATURAN_GL(a,b,temp,2,soal2_xtetap)
			f1 = temp
			!print*,"f1 = ",f1
			x_tetap = ((a + b) + (b - a) * (-sqrt(3.D0)/3)) / 2
			call ATURAN_GL(a,b,temp,2,soal2_xtetap)
			f2 = temp
			!print*,"f2 = ",f2
			hasil = (b-a)*(f1+f2)/2
			print*,"Solusi ",hasil
	end select
end


subroutine hitung_soal5_rekayasa(metode, n)
! metode 1-5
! n jumlah pias
	implicit none
	integer*2 metode, n
	double precision soal5_rekayasa, constant_T, hasil
	double precision a, b
	external soal5_rekayasa,ATURAN_TRAPESIUM,simpson_1_per_3
	external ROMBERG,ATURAN_GL
	
	a = 0
	constant_T = 1.0
	b = constant_T
	
	select case (metode)
		case(1)
			!metode trapesium
			!integral dari 0 - T
			print*,"Metode Aturan Trapesium"
			call ATURAN_TRAPESIUM(a,b,n,hasil,soal5_rekayasa)
			print*,"Solusi = ",hasil
		case(2)
			if (mod(n,2).eq.0) then
				print*,"Metode Aturan 1/3 Simpson"
				call simpson_1_per_3(a,b,n,hasil,soal5_rekayasa)
				print*,"Solusi = ",hasil
			else
				print*,"Nilai n = ",n," tidak bernilai genap"
			end if
		case(3)
			print*,"Metode Romberg"
			call ROMBERG(a,b,n,hasil,soal5_rekayasa)
			print*,"Solusi = ",hasil
		case(4)
			print*,"Metode Gauss Legendre 3 titik"
			call ATURAN_GL(a,b,hasil,3,soal5_rekayasa)
			print*,"Solusi = ",hasil
		case(5)
			print*,"Metode Gauss Legendre 4 titik"
			call ATURAN_GL(a,b,hasil,4,soal5_rekayasa)
			print*,"Solusi = ",hasil
	end select
end

subroutine hitung_soal3_rekayasa(metode,a,b)
! metode 1-5
! n jumlah pias
	implicit none
	integer*2 metode,a,b,r
	integer*2 tsiang(10), tmalam(10)
	double precision I1,I2,sigma1,sigma2,hasil
	
	tsiang(1) = 74
	tsiang(2) = 85
	tsiang(3) = 106
	tsiang(4) = 115
	tsiang(5) = 94
	tsiang(6) = 88
	tsiang(7) = 91
	tsiang(8) = 110
	tsiang(9) = 123
	tsiang(10) = 142
	
	tmalam(1) = 153
	tmalam(2) = 121
	tmalam(3) = 80
	tmalam(4) = 77
	tmalam(5) = 54
	tmalam(6) = 42
	tmalam(7) = 38
	tmalam(8) = 34
	tmalam(9) = 45
	tmalam(10) = 66
	
	select case (metode)
		case(1)
			!metode trapesium
			print*,"Metode Aturan Trapesium"
			I1 = tsiang(a)+tsiang(b)
			I2 = tmalam(a)+tmalam(b)
			sigma1 = 0
			sigma2 = 0
			do r=a+1,b-1
				sigma1 = sigma1 + 2*tsiang(r)
				sigma2 = sigma2 + 2*tmalam(r)
			end do
			I1 = (I1+sigma1)*1/2
			I2 = (I2+sigma2)*1/2
			hasil = I1+I2
			print*,"Solusi = ",hasil
		case(2)
			!metode aturan 1/3 Simpson
			r = b-a+1
			if (mod(r,2).eq.0) then
				print*,"Metode Aturan 1/3 Simpson"
				I1 = tsiang(a)+tsiang(b)
				I2 = tmalam(a)+tmalam(b)
				sigma1 = 0
				sigma2 = 0
				do r=a+1,b-1
					if (mod(r,2).eq.1) then
						sigma1 = sigma1 + 4*tsiang(r)
						sigma2 = sigma2 + 4*tmalam(r)
					else
						sigma1 = sigma1 + 2*tsiang(r)
						sigma2 = sigma2 + 2*tmalam(r)
					end if
				end do
				I1 = (I1+sigma1)*1/3
				I2 = (I2+sigma2)*1/3
				hasil = I1+I2
				print*,"Solusi = ",hasil
			else
				print*,"Data pada interval a,b tidak berjumlah genap."
			end if
		case(3)
			r = b-a+1
			if (mod(r,3).eq.0) then
				print*,"Metode Aturan 3/8 Simpson"
				I1 = tsiang(a)+tsiang(b)
				I2 = tmalam(a)+tmalam(b)
				sigma1 = 0
				sigma2 = 0
				do r=a+1,b-1
					if (mod(r,3).eq.0) then
						sigma1 = sigma1 + 2*tsiang(r)
						sigma2 = sigma2 + 2*tmalam(r)
					else
						sigma1 = sigma1 + 3*tsiang(r)
						sigma2 = sigma2 + 3*tmalam(r)
					end if
				end do
				I1 = (I1+sigma1)*3/8
				I2 = (I2+sigma2)*3/8
				hasil = I1+I2
				print*,"Solusi = ",hasil
			else
				print*,"Data pada interval a,b tidak berjumlah kelipatan tiga."
			end if
	end select
end

